﻿@using LanDBom.Resources
@using LanDBom.Models

@{
    ViewBag.Title = "Purchasing_Authority";
}

<h2>采购权限管理</h2>

<from>
    <div>
        <fieldset class="boun nav" style="padding-bottom:6px;">
            <legend class="len">@Resource.String1235</legend>
            <div class="col-lg-3">@Resource.String1236:<input type="text" style="width:150px;" id="txtid" /></div>
            <div class="col-lg-3">@Resource.String1237:<input type="text" style="width: 150px;" id="txtname" /></div>
            <div class="col-lg-3">@Resource.String1238:<input type="text" style="width: 150px;" id="txtstanding" /></div>
            <div calss="col-lg-1"><button id="find_E" class="btn-sm  btn-secondary">@Resource.String1239</button></div>
        </fieldset>
    </div>
    <div>
        <button id="Tijanm">添加物料分类</button>
        <button id="QH">切换明细页</button>
    </div>
    <div>
        <div id="One_1">
            <table class="table" style="text-align:center">
                <thead class="bg-light">
                    <tr>
                        <td>@Resource.String1241</td>
                        <td>@Resource.String1243</td>
                        <td>部门</td>
                        <td>管理</td>
                    </tr>
                </thead>
                <tbody id="E_tb_e1" style="height:500px">
                </tbody>
            </table>
        </div>
        <div id="One_2" style="display:none;max-height:600px; overflow-y:scroll">
            <table class="table" style="text-align:center">
                <thead>
                    <tr>
                        <td>@Resource.String1241</td>
                        <td>@Resource.String1243</td>
                        <td>部门</td>
                        <td>物料分类</td>
                    </tr>
                </thead>
                <tbody id="E_tb_e2">
                </tbody>
            </table>
        </div>
    </div>

    <div id="show_shangc" style="display: none; position: absolute; border: 1px black solid; left: 25%; top: 33%; background-color: white; background-color: #f1f1f1;width:500px;height:300px">
        <div style="text-align: right; background-color: #84acde;"><span class="QUED_quxiao" style=" margin: 20px;"><i class="bi bi-x-square"></i></span></div>
        <div style="background-color: #fff;height:80%;overflow:scroll">
            <div id="DE_id" style="display:none"></div>
            <table style="width:100%">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>负责人</th>
                        <th>物料分类</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="CG_qxxs">
                </tbody>
            </table>
        </div>
        <div style="background-color: #fff;">
            <div id="DE_id" style="display:none"></div>
            <table style="width:100%">
                <tfoot>
                    <tr>
                        <td id="name"></td>
                        <td>
                            <select id="dl_wl">
                                <option value="0">请选择</option>
                                @foreach (var item in ViewBag.temp2 as List<buty3>)
                                {
                                    <option value="@item.zl2_id">@item.zl2_name</option>
                                }
                            </select>
                        </td>
                        <td>
                            <select id="wl_xl">
                            </select>
                        </td>
                        <td>
                            <a id="tianj" href="#">添加</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div style="text-align:center;height:10%"><button class="QUED_quxiao">@Resource.String126</button></div>
    </div>
    <div id="JJNNK" style="display: none; position: absolute; border: 1px black solid; left: 25%; top: 33%; background-color: white; background-color: #f1f1f1;width:500px;height:300px">
        <div style="text-align: right; background-color: #84acde;"></div>
        <div style="background-color: #fff;height:80%;overflow:scroll">
            <table style="width:100%">
                <thead>
                    <tr>
                        <th>一级分类</th>
                        <th>二级分类</th>
                    </tr>
                </thead>
                <tbody id="CG_fenL">
                </tbody>
            </table>
        </div>
        <div style="background-color: #fff;">
            <table style="width:100%">
                <tfoot>
                    <tr>
                        <td>
                            一级类别：
                            <select id="dl_wlED">
                                <option value="0">请选择</option>
                                @foreach (var item in ViewBag.temp2 as List<buty3>)
                                {
                                    <option value="@item.zl2_id">@item.zl2_name</option>
                                }
                            </select>
                        </td>
                        <td>
                            二级类别：
                            <input type="text" value="" id="CG_jian" />
                        </td>
                        <td>
                            <a id="tianjian" href="#">添加</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>

        <div style="text-align:center;height:10%"><button class="QUED_quxiao">@Resource.String126</button></div>
    </div>
</from>

@section scripts
{
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script>
        $(function () {
            GetEmployee()
            //打开权限管理
            $(document).on("click", "#CG_qxgl", function (e) {
                var id = $(e.target).parent().prev().text()
                var name = $(e.target).parent().parent().children().eq(0).text()
                console.log(id, name)
                $("#show_shangc").css({ "display": "block" })
                $("#DE_id").text(id)
                $("#name").text(name)
                Getquanx()
            })

            //关闭权限管理
            $(document).on("click", ".QUED_quxiao", function (e) {
                $("#show_shangc").css({ "display": "none" })
                $("#JJNNK").hide()
                $("#DE_id").text("")
            })
            //添加物料分类
            $(document).on("click", "#Tijanm", function (e) {
                $("#JJNNK").show()
            })
            $(document).on("click", "#QH", function (e) {
                if ($("#QH").text() == "切换明细页") {
                    $("#One_1").hide()
                    $("#One_2").show()
                    $("#QH").text("切换管理页")
                } else {
                    $("#One_1").show()
                    $("#One_2").hide()
                    $("#QH").text("切换明细页")
                }
            })


            //删除权限
            $(document).on("click", "#CG_scqx", function (e) {
                var id = $(e.target).parent().parent().children().eq(0).text()
                $.ajax({
                    url: '/Admin/Purchasing_Authority',
                    type: 'GET',
                    data: { "id": id, "state": 3 },
                    success: function (data) {
                        alert(data)
                        Getquanx()
                    },
                    error: function () {
                        alert('Request Error');
                        Getquanx()
                    }
                })
            })

            //添加权限
            $(document).on("click", "#tianj", function (e) {
                var name = $("#wl_xl").val()
                var id = $("#DE_id").text()
                var BZJG = []
                BZJG.push({
                    dl_id: id,
                    du_name: name
                })
                console.log("共" + BZJG.length + "数据");
                console.log(BZJG);
                $.ajax({
                    url: "/Admin/AddNdardo_CP",
                    type: 'POST',
                    data: JSON.stringify(BZJG),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (text) {
                        alert(text)
                        Getquanx()
                    },
                    error: function (text) {
                        alert("系统错误" + text);
                    }
                })
            })


            $(document).on("change", "#dl_wl", function (e) {
                var id = $(e.target).val()
                $.ajax({
                    url: '/Admin/Purchasing_Authority',
                    type: 'GET',
                    data: { "id": id, "state": 4 },
                    success: function (data) {
                        $("#wl_xl option").remove()
                        $.each(data, function (i, item) {
                            $("#wl_xl").append($(`<option value="${item.zl_name}">${item.zl_name}</option>`))
                        })
                    },
                    error: function () {
                        alert('Request Error');
                        Getquanx()
                    }
                })
            })
            //搜索权限的方法
            function Getquanx() {
                $.ajax({
                    url: '/Admin/Purchasing_Authority',
                    type: 'GET',
                    data: { "id": $("#DE_id").text(), "state": 2 },
                    success: function (data) {
                        createRow_2(data)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }

            //打印权限的方法
            function createRow_2(data) {
                $('#CG_qxxs tr').remove();
                var index = 1
                $.each(data, function (i, item) {
                    var $tr = $(`
                                                        <tr>
                                                            <td id="E_name_e1" style="display:none">${item.du_id}</td>
                                                            <td id="E_name_e1">${index}</td>
                                                            <td id="E_name_e1">${item.dl_id}</td>
                                                            <td id="E_shenf_e1">${item.du_name}</td>
                                                            <td><a href="#" id="CG_scqx">删除</a></td>
                                                        </tr>
                                                    `);
                    $('#CG_qxxs').append($tr);
                    index++
                })
            }

            //查找员工的方法
            function GetEmployee() {
                var name = $("#txtname").val()
                var id = $("#txtid").val()
                var standing = $("#txtstanding").val()
                $.ajax({
                    url: '/Admin/Purchasing_Authority',
                    type: 'GET',
                    data: { "id": id, "name": name, "standing": standing, "state": 1 },
                    success: function (data) {
                        createRow(data)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            //打印员工的方法
            function createRow(data) {
                $('#E_tb_e1 tr').remove();
                $.each(data, function (i, item) {
                    var $tr = $(`
                                                                    <tr>
                                                                        <td id="E_name_e1">${item.DE_nae}</td>
                                                                        <td id="E_shenf_e1">${item.DE_quan}</td>
                                                                        <td id="E_bu_e1">${item.FName}</td>
                                                                        <td style="display:none" id="E_id_e1">${item.DE_ID}</td>
                                                                        <td><a href="#" id="CG_qxgl">管理</a></td>
                                                                    </tr>
                                                                `);
                    $('#E_tb_e1').append($tr);
                })
            }
            //添加物料分类
            $(document).on("click", "#tianjian", function () {
                var buty2 = []
                buty2.push({
                    zl2_id: $("#dl_wlED").val(),
                    zl_name: $("#CG_jian").val()
                })
                console.log(buty2)
                if ($("#dl_wlED").val() == "0") {
                    alert("请选择一级类别！")

                } else {
                    $.ajax({
                        url: '/Admin/AddNdardo_',
                        type: 'POST',
                        data: JSON.stringify(buty2),
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            alert(data)
                            JiaZ()
                        },
                        error: function () {
                            alert('Request Error');
                        }
                    })
                }

            })
            //显示已保存的分类
            JiaZ()
            function JiaZ() {
                $.ajax({
                    url: '/Admin/Purchasing_Authority',
                    type: 'GET',
                    data: { "state": 6 },
                    success: function (data) {
                        shubd(data)
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            }
            function shubd(dde) {
                $('#CG_fenL tr').remove();
                $.each(dde, function (i, item) {
                    var temp7 = item.zl2_id
                    temp7 = temp7 == "1" ? "纸质" : temp7 == "2" ? "木质" : temp7 == "3" ? "塑料" : temp7 == "4" ? "金属制品" : temp7 == "5" ? "布" : temp7 == "6" ? "胶粘制品" : temp7 == "7" ? "其他" : temp7 == "8" ? "套件" : temp7;
                    var $tr = $(`
                                                <tr>
                                                <td style="display:none">${item.zl_id}</td>
                                                    <td id="E_name_e1">${temp7}</td>zl_id
                                                    <td id="E_shenf_e1">${item.zl_name}</td>
                                                    <td><a href="#" id="CG_Del">删除</a></td>
                                                </tr>
                                            `);
                    $('#CG_fenL').append($tr);
                })
            }
            $(document).on("click", "#CG_Del", function () {
                if (confirm("删除该物料分类，人员分类同时删除！")) {
                    var jin = $(this).parent().parent().children().eq(0).text();
                    console.log(jin)
                    $.ajax({
                        url: '/Admin/Purchasing_Authority',
                        type: 'GET',
                        data: { "state": 7, "id": jin },
                        success: function (data) {
                            alert(data)
                            JiaZ()
                        },
                        error: function () {
                            alert('Request Error');
                        }
                    })
                }
            })
            $(document).ready(function () {
                //填充
                $.ajax({
                    url: '/Admin/Purchasing_Authority',
                    type: 'GET',
                    data: { "state": 8 },
                    success: function (data) {
                        $.each(data, function (i, item) {
                            var $tr = $(`
                                        <tr>
                                            <td >${item.DE_nae}</td>
                                            <td >${item.DE_quan}</td>
                                            <td >${item.F_BuMen}</td>
                                            <td>${item.du_name}</td>
                                            <td style="display:none" id="E_id_e1">${item.DE_ID}</td>
                                        </tr>
                                                `);
                            $('#E_tb_e2').append($tr);
                        })
                    },
                    error: function () {
                        alert('Request Error');
                    }
                })
            })
        })
    </script>
}